﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace MakePICCLib
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            FileInfo f;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                checkedListBox1.Items.Clear();
                f = new FileInfo(openFileDialog1.FileName); 
                toolStripLabel4.Text = f.DirectoryName;
                foreach (string file in openFileDialog1.FileNames)
                {
                    f = new FileInfo(file); 
                    checkedListBox1.Items.Add(f.Name, true);
                }
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            FileInfo f;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                toolStripTextBox2.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripComboBox1.SelectedIndex = 0;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (toolStripTextBox2.Text == "")
            {
                MessageBox.Show("请指定输出目录");
                return;
            }
            if (checkedListBox1.Items.Count == 0)
            {
                MessageBox.Show("请指定程序文件");
                return;
            }
            foreach (object o in checkedListBox1.Items)
            {
                string f = toolStripLabel4.Text + "\\" + (string)o;
                Process process = new Process();     //创建进程对象               
                ProcessStartInfo startInfo = new ProcessStartInfo();
                if (toolStripComboBox1.SelectedIndex == 0)
                {
                    startInfo.FileName = System.Windows.Forms.Application.StartupPath+"\\libr_16.exe";      //设定需要执行的命令
                    startInfo.Arguments = "r " + toolStripTextBox2.Text + "16" + ((string)o).Substring(0, ((string)o).IndexOf('.')) + ".lpp " + f;   //设定参数，其中的“/C”表示执行完命令后马上退出   
                }
                else if (toolStripComboBox1.SelectedIndex == 1)
                {
                    startInfo.FileName = System.Windows.Forms.Application.StartupPath + "\\libr_18.exe";      //设定需要执行的命令
                    startInfo.Arguments = "r " + toolStripTextBox2.Text + "\\18" + ((string)o).Substring(0, ((string)o).IndexOf('.')) + ".lpp " + f;   //设定参数，其中的“/C”表示执行完命令后马上退出   
                }
                startInfo.UseShellExecute = false;     //不使用系统外壳程序启动                
                startInfo.RedirectStandardInput = false;   //不重定向输入                
                startInfo.RedirectStandardOutput = true;   //重定向输出                
                startInfo.CreateNoWindow = true;     //不创建窗口                
                process.StartInfo = startInfo;                
                try                
                {                    
                    if (process.Start())       //开始进程                    
                    {
                        textBox1.AppendText("开始生成" + (string)o + "库文件" + "\r\n");
                        if (process.WaitForExit(5000)) //这里等待进程结束，等待时间为指定的毫秒     
                        {
                            textBox1.AppendText("生成" + (string)o + "库文件成功" + "\r\n");
                            textBox1.AppendText(process.StandardOutput.ReadToEnd());
                            textBox1.AppendText("----------------------------------------\r\n");
                        }
                        else
                        {
                            textBox1.AppendText("生成" + (string)o + "库文件失败" + "\r\n");
                            textBox1.AppendText(process.StandardOutput.ReadToEnd());
                            textBox1.AppendText("----------------------------------------\r\n");
                        }
                    }                
                }                
                catch                
                {
                    textBox1.AppendText("生成" + (string)o + "库文件出错" + "\r\n");
                    textBox1.AppendText(process.StandardOutput.ReadToEnd());
                    textBox1.AppendText("----------------------------------------\r\n");
                }                
                finally                
                {                    
                    if (process != null)               
                        process.Close();        
                }
            }
        }
        
    }

        
}
